package components.utils
{
	import capricornus.components.button.Button;
	import capricornus.components.singleselectgroup.SingleSelectGroup;
	import capricornus.components.utils.AssetTool;
	
	import fl.transitions.Tween;
	import fl.transitions.easing.Regular;
	
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;

	public class RollShowBarController extends EventDispatcher
	{
		private var _target:Sprite;
		
		private var _show:MovieClip;
		
		private var _bar:Sprite;
		private var _mask:Shape;
		
		private var _tween:Tween;
		private var _right:int;
		
		private var _group:SingleSelectGroup;
		private var _vecBtn:Vector.<Button>;
		
		private var _isClickToShow:Boolean = false;
		
		public function RollShowBarController( target:Sprite, isClickToShow:Boolean=false )
		{
			_target = target;
			_isClickToShow = isClickToShow;
			init();
		}
		
		private function init():void
		{
			_bar = _target.getChildByName("bar") as Sprite;
			_show = _target.getChildByName("show") as MovieClip;
			_show.buttonMode = _isClickToShow;
			_show.mouseChildren = false;
			_show.gotoAndStop(1);
			
			_mask = new Shape();
			_mask.graphics.beginFill(0);
			_mask.graphics.drawRect( 0, 0, _bar.width, _bar.height );
			_mask.graphics.endFill();
			_target.addChild( _mask );
			
			_bar.mouseEnabled =  false;
			_bar.mask = _mask;
			_mask.y = _bar.y;
			_mask.x = -_mask.width;
			
			_right = _bar.x;
			
			_group = new SingleSelectGroup();
			_vecBtn = new Vector.<Button>();
			var i:int = 0;
			while( true )
			{
				var btn:SimpleButton = _bar.getChildByName("s"+i) as SimpleButton;
				i++;
				if( btn==null ) break;
				var button:Button = new Button();
				AssetTool.aplyButton( button, btn );
				button.mouseChildren = false;
				button.mouseEnabled = true;
				button.name = String(i-1);
				_vecBtn.push( button );
				_group.addItem( button );
			}
			
			if( _isClickToShow )
			{
				_show.addEventListener( MouseEvent.CLICK, onClickShow, false, 0, true );
			}
			else
			{
				_target.addEventListener( MouseEvent.ROLL_OVER, onRollOver, false, 0, true );
				_target.addEventListener( MouseEvent.ROLL_OUT, onRollOut, false, 0, true );
			}
			
			_bar.addEventListener( MouseEvent.CLICK, onClickBar, false, 0, true );
			
			_tween = new Tween( _mask, "x", Regular.easeOut, _mask.x, _mask.x, 0.1, true );
			_tween.stop();
		}
		
		private var _isShowing:Boolean = false;
		private function onClickShow( e:MouseEvent ):void
		{
			_isShowing = !_isShowing;
			if( _isShowing )
			{
				onRollOver( null );
			}
			else
			{
				onRollOut( null );
			}
		}
		
		private function onRollOver( e:MouseEvent ):void
		{
			_tween.begin = _mask.x;
			_tween.finish = _right;
			_tween.start();
		}
		
		private function onRollOut( e:MouseEvent ):void
		{
			_tween.begin = _mask.x;
			_tween.finish = -_mask.width;
			_tween.start();
		}
		
		private function onClickBar( e:MouseEvent ):void
		{
			onRollOut(null);
			
			var btn:Button = e.target as Button;
			if( btn != null )
			{
				var index:int = int(btn.name);
				_show.gotoAndStop( index+1 );
			}
		}
	}
}


















